<?php

class File_VFS extends Object
{
    var $_interfaces;
    var $_order = array('file', 'ftp');    
    var $_cwd;
    
    function __construct()
    {
        // Set working directory
        
        /*
         * Instantiate all the available VFS file
         * interfaces.
         */
    }
    
    /**
     * Make Directory
     *
     * @param string $path  Location and name of the directory to be created.
     * 
     * @return bool
     */
    function mkdir($path)
    {        
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->mkdir($path, $name) !== false)
                return true;
        }
        
        return false;
    }
    
    /**
     * Move File or Directory
     * 
     * Moves the source file or directory to the specified destination, if
     * the last character of destination is a / then the original name is kept otherwise
     * the name after the / is used.
     *
     * @param string $source       Source file or directory
     * @param string $dest         Destination for file or directory
     * @param bool   $autocreate   Recursively create destination if needed
     * 
     * @return bool
     */
    function mv($source, $dest, $autocreate = true)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->mv($source, $dest, $autocreate) !== false)
                return true;
        }
        
        return false;        
    }
    
    /**
     * Copy file or directory
     * 
     * Same as move but the source file or directory remains 
     * untouched.
     *
     * @param string $source       Source file or directory
     * @param string $dest         Destination for file or directory
     * @param bool   $autocreate   Recursively create destination if needed
     * 
     * @return bool
     */
    function cp($source, $dest, $autocreate = true)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->cp($source, $dest, $autocreate) !== false)
                return true;
        }
        
        return false;         
    }
    
    /**
     * Delete File or Directory
     *
     * This method has no mercy, think of it like the linux rm command
     * with the -rf flags specified.
     * 
     * @param string $location  Location of the file or directory to be deleted
     * 
     * @return bool
     */
    function rm($location)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->rm($location) !== false)
                return true;
        }
        
        return false;           
    }
    
    /**
     * Change File or Directory Permissions
     *
     * Permission Strings
     *   Standard chmod syntax with the following implemented.
     * 
     *   Who :-
     *     u - User
     *     g - Group
     *     a - All
     * 
     *   Opcode :-
     *     + - Add Permission
     *     - - Remove Permission
     *     = - Assign permission (and remove permission of the unspecified fields)
     * 
     *   Permissions :-
     *     r - Read
     *     w - Write
     *     x - Execute
     * 
     *   Alternatively the equivalent Octal code can be specified.
     * 
     *   Lastly simply supplying one of the permissions or a mix of them
     *   eg. just 'r' or 'rw' will give the minimal access possible for the
     *   webserver to be allowed to perform the specifed permission.
     * 
     * @param string $location    Location fo the file or directory
     * @param string $permission  Permission to be used
     * 
     * @return bool
     */
    function chmod($location, $permission)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->chmod($location, $permission) !== false)
                return true;
        }
        
        return false;          
    }
    
    /**
     * Enter description here...
     *
     * @return unknown
     */
    function cwd()
    {
        return $this->_cwd;
    }
    
    /**
     * Enter description here...
     *
     * @param unknown_type $location
     * @return unknown
     */
    function ls($location)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->ls($location) !== false)
                return true;
        }
        
        return false;         
    }
    
    /**
     * Enter description here...
     *
     * @param unknown_type $file
     * @return unknown
     */
    function read($file)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->read($file) !== false)
                return true;
        }
        
        return false;           
    }
    
    /**
     * Enter description here...
     *
     * @param unknown_type $file
     * @param unknown_type $data
     * @return unknown
     */
    function write($file, $data)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->write($file, $data) !== false)
                return true;
        }
        
        return false;           
    }

    /**
     * Enter description here...
     *
     * @param unknown_type $location
     * @return unknown
     */
    function size($location)
    {
        for ($i = 0, $m = count($this->_order); $i < $m; $i++)
        {
            if ($this->_interfaces[$this->_order[$i]]->size($location) !== false)
                return true;
        }
        
        return false;         
    }
}

/*
 * Establish an VFS instance using the warlock root as the VFS root and
 * provide some short cut functions for all the different file operations.
 */

$vfs = new File_VFS(W_ENV_PATH);

function vfs_mkdir($path)
{
    global $vfs;    
    return $vfs->mkdir($path);
}

function vfs_mv($source, $dest, $autocreate = true)
{
    global $vfs;
    return $vfs->mv($source, $dest, $autocreate);
}

function vfs_cp($source, $dest, $autocreate = true)
{
    global $vfs;
    return $vfs->cp($source, $dest, $autocreate);
}

function vfs_rm($location)
{
    global $vfs;
    return $vfs->rm($location);
}

function vfs_chmod($location, $permission)
{
    global $vfs;
    return $vfs->chmod($location, $permission);
}

function vfs_cwd()
{
    global $vfs;
    return $vfs->cwd();
}

function vfs_ls($location)
{
    global $vfs;
    return $vfs->ls($location);
}

function vfs_read($file)
{
    global $vfs;
    return $vfs->read($file);
}

function vfs_write($file, $data)
{
    global $vfs;
    return $vfs->write($file, $data);
}

?>